<?php

declare(strict_types=1);
/**
 * 作者：薛原
 * 功能：管理后台登录认证控制器
 * 时间：2025/10/17
 */

namespace App\Controller\Admin;

use App\Service\UserService1;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Contract\RequestInterface;


#[Controller(prefix: "/admin/auth")]

class AuthController extends AbstractController
{
    #[Inject]
    private UserService1 $userService;

    /**
     * 管理员登录接口
     * @param RequestInterface $request
     * @return array|\Psr\Http\Message\ResponseInterface
     */
    #[PostMapping("login")]
    public function login(RequestInterface $request)
    {
        $params = $this->parseParams($request);
        if(!isset($params['email']) || !isset($params['password'])){
            return $this->fail(40002,"登录失败",["errors"=>"入参不全"]);
        }
        $email = $params['email'];
        $password = $params["password"];
        $server_info = $request->getServerParams();
        $ip = $server_info['remote_addr'];
        try{
            $ret = $this->userService->login($email,$password, $ip, "admin");
        }catch (\App\Exception\BusinessException $e){
            return $this->fail(40002,"登录失败",["errors"=>$e->getMessage()]);
        }
        return $this->success($ret,"登录成功");
    }
}
